<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var countPalindromicSubsequence = function (s) {
      let ans = 0
      let l = 0, r = s.length - 1
      let visitor = new Set()

      while (l < s.length - 2) {
        if (!visitor.has(s[l])) {
          visitor.add(s[l])
          r = s.length - 1
          while (s[l] !== s[r]) r--;
          r--
          let v = new Set()
          while (l < r) {
            if (!v.has(s[r])) {
              v.add(s[r])
              ans++
            }
            r--
          }

        }
        l++
      }
      return ans
    }
    console.log(countPalindromicSubsequence("aabca"))
  </script>
</body>

</html>